<template>
    <div>
        <div class="crumbs">
            <el-page-header @back="goBack" content="答题记录">
            </el-page-header>
        </div>
        <div class="container">
            <div class="handle-box">
                
                <!-- <el-input v-model="query.content" placeholder="竞赛名称" class="handle-input mr10"></el-input> -->
                <el-input v-model="query.questionType" placeholder="题型" class="handle-input mr10"></el-input>
                
                <el-input v-model="query.type" placeholder="类型" class="handle-input mr10"></el-input>
                <!-- <el-input v-model="query.score" placeholder="得分" class="handle-input mr10"></el-input> -->
                <el-button type="primary" style="margin-right: 5px;" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <!-- <el-table-column prop="id" label="id"></el-table-column> -->
                <!-- <el-table-column prop="studentId" label="学号"></el-table-column> -->
                <el-table-column prop="content" label="题目描述"></el-table-column>
                <el-table-column prop="questionType" label="题型"></el-table-column>
                <el-table-column prop="type" label="类型"></el-table-column>
                <el-table-column label="正确率">
                    <template slot-scope="scope">{{scope.row.totalCnt == 0 ? 0 : (scope.row.correctCnt/scope.row.totalCnt).toFixed(3)}}</template>
                </el-table-column>

                <!-- <el-table-column prop="updateTime" label="更新时间"></el-table-column> -->
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.current"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
import { getUserScoreList, deleteUserScore, getLoginUser, updateUserScore,listUserQuestionByPage } from '../../api/index';
export default {
    name: 'questionRecordsTable',
    data() {
        return {
            query: {
                current: 1,
                pageSize: 10,
                studentId:''
            },
            tableData: [],
            multipleSelection: [],
            allData: [],
            delList: [],
            editVisible: false,
            addVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1,
            
        };
    },
    created() {
        getLoginUser().then(response => {
            if(response.code == 40100){
                    this.$message.error('未登录，请先登录！')
                    this.$router.push('/login')
                }else{
                    let res = response.data
                    this.query.studentId = res.studentID
                    this.getData();
                }
        })
    },
    methods: {
        getData() {
            listUserQuestionByPage(this.query).then(response => {
                console.log(response)
                if(response.code != 0){
                    this.$message.error(response.message)
                }else{
                    let res = response.data
                    this.tableData = res.records;
                    this.pageTotal = res.total;
                }
            });
            
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'current', 1);
            this.getData();
        },
        goBack(){
            this.$router.push('/dashboard')
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    deleteUserScore(row).then( res => {
                        console.log(res)
                    }).then(()=>{
                        this.$message.success('删除成功');
                        this.tableData.splice(index, 1);
                        this.pageTotal-=1
                    }).catch(err => {
                        this.$message.error('删除失败！')
                    })
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'current', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
    display: flex;
}

.handle-select {
    width: 120px;
}

.formSelect {
    width: 100%;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
